package com.csun.cmny.provider.device.card;

import com.alibaba.fastjson.JSON;
import com.csun.cmny.provider.device.card.entity.CardCloseDoorCmdEntity;
import com.csun.cmny.provider.device.card.entity.CardOpenDoorCmdEntity;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-11-28 20:43
 * @Description:
 */
public class CardMessageEncoder {

    public static CardOpenDoorCmdEntity buildOpenDoorCmd(String deviceNo, byte[] password, byte[] code, byte[] doors) {

        return new CardOpenDoorCmdEntity(deviceNo, password, code, doors);
    }

    public static CardCloseDoorCmdEntity buildCloseDoorCmd(String deviceNo, byte[] password, byte[] code, byte[] doors) {

        return new CardCloseDoorCmdEntity(deviceNo, password, code, doors);
    }


    @Test
    public void main() {

        byte[] password = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(0xffffffff).array();
        byte[] code = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(0xffffffff).array();
        // byte[] doors = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(0x01000000).array();

        byte[] doors = new byte[4];
        doors[0] = 1;
        doors[1] = 0;
        doors[2] = 0;
        doors[3] = 0;

        CardOpenDoorCmdEntity openCmd = buildOpenDoorCmd("CU-BH80117070815", password, code, doors);

        byte[] o =  openCmd.getBytes();

        String ret = format(Hex.encodeHexString(o));

        System.out.println("openCmd = " + ret);

        CardCloseDoorCmdEntity closeCmd = buildCloseDoorCmd("CU-BH80117070815", password, code, doors);

        byte[] c =  closeCmd.getBytes();

        ret = format(Hex.encodeHexString(c));

        System.out.println("closeCmd = " + ret);
    }

    public static String format(String str) {

        String regex = "(.{2})";
        str = str.replaceAll(regex, "$1 ");
        return str;
    }

    @Test
    public void deviceNo() {

        String sn = "CU-BH80117070815";
        String ret = format(Hex.encodeHexString(sn.getBytes()));
        // 43 55 2D 42 48 38 30 31 31 37 30 37 30 38 31 35
        System.out.println("sn = " + ret);

        byte[] password = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(0xffffffff).array();
        ret = format(Hex.encodeHexString(password));
        System.out.println("password = " + ret);

        byte[] code = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(0xffffffff).array();
        ret = format(Hex.encodeHexString(code));
        System.out.println("code = " + ret);

        byte[] doors = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(0x01000000).array();
        ret = format(Hex.encodeHexString(doors));
        System.out.println("doors = " + ret);
    }
}
